﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AcroPDFLib;
using DataTier;
using BusinessTier;

namespace GUI
{
    public partial class ReviewForm : Form
    {
        private Expense _expense;
        private List<Expense> _expenses;
        private int _index = -1;
        private MainForm _mainForm = MainForm.Instance;
        private bool _edited = false;
        private ExpenseContainer _expCnt = ExpenseContainer.Instance;
        private SapConnectionCtr _sapConnectionCtr = SapConnectionCtr.Instance;
        private ExpenseCtr _expenseCtr = ExpenseCtr.Instance;

        private static ReviewForm instance = null;

        private ReviewForm(List<Expense> expenses)
        {
            InitializeComponent();
            _expenses = expenses;
            GetInformation(); //Shows the information for the first Expense in the list
            this.WindowState = FormWindowState.Maximized;
            lblCounter1.Text = "1";
            lblCounter2.Text = _expenses.Count.ToString();
        }



        public static ReviewForm Instance(List<Expense> expenses)
        {
            if (instance == null)
            {
                instance = new ReviewForm(expenses);
            }
            return instance;
        }

     /// <summary>
     /// If any changes has been made to any expenses, this method sends the changed expenses back to SAP,
     /// so changes can be saved.
     /// It also resets the pdf-reader, and shuts down the reviewform
     /// </summary>
     /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            pdfViewer.Dispose();
            pdfViewer = null;
            _index = -1;
            if (_expCnt._editedExpenses.Count > 0)
            {
                _expenseCtr.InsertEditedExpenses(); //Tries to insert new/update old expenses in SAP
                _expCnt._editedExpenses.Clear(); //Clears the editedlist if they are inserted correctly into SAP
            }
            _mainForm.GetAllExpenses();
            instance = null;
        }

        /// <summary>
        /// Shows all the information, one at the time, for every expenses in the list (Including the receipt (PDF))
        /// When done, it updates SAP, and clears the list with edited expenses,
        /// </summary>
        private void GetInformation()
        {
            _index++;
            _edited = false;

            try
            {
                if (_index <= _expenses.Count() - 1)
                {
                    _expense = _expenses[_index];
                    showPDF(); //Shows the PDF corresponding to the current expense
                    ShowInformation(); //Shows the current expense in the winform
                }

                else
                {
                    if (_expCnt._editedExpenses.Count > 0)
                    {
                        _expenseCtr.InsertEditedExpenses(); //Tries to insert new/update old expenses in SAP
                        _expCnt._editedExpenses.Clear(); //Clears the editedlist if they are inserted correctly into SAP
                    }

                    MessageBox.Show("Alle valgte bilag er efterset");
                    this.Close();
                }

            }

            catch (Exception e)
            {
                MessageBox.Show("Der er sket en fejl: " + e.Message);
            }
        }

        /*
         * Godkendt
         */
        private void showPDF()
        {
            string filePath = _expense.pictureURL;
            pdfViewer.LoadFile(filePath);
            pdfViewer.src = filePath;
            pdfViewer.setView("Fith");
            pdfViewer.setShowToolbar(false);
            pdfViewer.setLayoutMode("SinglePage");
            pdfViewer.Show();

        }
        /*
         * Godkendt
         */
        private void ShowInformation()
        {
            txtFirstName.Text = _expense.firstName;
            txtLastName.Text = _expense.lastName;
            txtDescription.Text = _expense.description;
            txtAmount.Text = _expense.convAmount;
            txtReceiptnr.Text = _expense.receiptNr;
            txtType.Text = _expense.expenseType;
            txtExpenseDate.Text = _expense.expenseDate;
            txtCreateDate.Text = _expense.createDate;
        }
        /*
        * Godkendt
        */
        private void btnApprove_Click(object sender, EventArgs e)
        {

            _expense.reviewed = "X";

            //if not edited (reviewed will always be edited, and doesn't count as "_edited  = true")
            if (_edited == false)
            {
                _expCnt._editedExpenses.Add(_expense);
                GetInformation();
            }

                //if "_edited = true" a clone of current expense will be cloned, and the edited attributes
            //will be changed in the cloneobject
            else
            {
                Expense newExpense = _expenseCtr.CopyExpense(_expense);

                newExpense.amount = txtAmount.Text;
                newExpense.description = txtDescription.Text;

                _expCnt._editedExpenses.Add(newExpense);
                _expCnt._editedExpenses.Add(_expense);
                GetInformation();

            }
            setUpdateCount();
        }

        /*
        * Godkendt
        */
        private void EnableEdit()
        {
            txtDescription.Enabled = true;
            txtAmount.Enabled = true;
            btnAccept.Enabled = false;
            btnEdit.Enabled = false;
            btnAcceptEdit.Visible = true;
            btnCancelEdit.Visible = true;
        }

        /*
        * Godkendt
        */
        private void DisableEdit()
        {
            txtDescription.Enabled = false;
            txtAmount.Enabled = false;
            btnAccept.Enabled = true;
            btnEdit.Enabled = true;
            btnAcceptEdit.Visible = false;
            btnCancelEdit.Visible = false;
        }

        /*
        * Godkendt
        */
        private void btnCancelEdit_Click(object sender, EventArgs e)
        {
            DisableEdit();
            _edited = false;
            txtDescription.Text = _expense.expenseDescription;
            txtAmount.Text = _expense.amount;
        }

        /*
        * Godkendt
        */
        private void btnAcceptEdit_Click(object sender, EventArgs e)
        {
            DisableEdit();
            _edited = true;
        }

        /*
        * Godkendt
        */
        private void btnEdit_Click(object sender, EventArgs e)
        {
            EnableEdit();
        }

        private void setUpdateCount()
        {
            int index = _index + 1;
            lblCounter1.Text = index.ToString();

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            GetInformation();
            setUpdateCount();
        }


    }
}
